﻿#include "stdafx.h"
#include "VisualizeDataServer.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMenu>
#include <QContextMenuEvent>
#include "customControl/AnimationCard/SurfaceMap.h"
#include "customControl/AnimationCard/DimensionMap.h"
#include "customControl/AnimationCard/DimensionTableView.h"
#include "title.h"
#include "QDebug"
#include "SocketSettingPage.h"

struct VisualizeDataServer::Internal
{
    Internal(VisualizeDataServer* ptr)
    {
        q = ptr;
        m_pTitle = new CTitle(q);
        m_pTitle->setObjectName(QStringLiteral("AppTitle"));
    }
    void initControl()
    {
        m_pMenu = new QMenu(q);
        m_pMenu->setParent(q);

        QList<QAction*> actionList;

		//<! 右键菜单 Socket设置
		QAction *pAction1 = new QAction("Socket设置");
		connect(pAction1, &QAction::triggered, q, &VisualizeDataServer::showSocketSetting);
		actionList.append(pAction1);

		//<! 右键菜单
		QAction* pAction = new QAction(tr("退出程序"), q);
		connect(pAction, &QAction::triggered, q, &VisualizeDataServer::exitApp);
		actionList.append(pAction);
		m_pMenu->addActions(actionList);
		q->setContextMenuPolicy(Qt::DefaultContextMenu);

        //<! 程序主界面布局
        QVBoxLayout* layoutMain = new QVBoxLayout(q);
        layoutMain->setMargin(0);
        layoutMain->setSpacing(0);

		//瑕疵地图
        QVBoxLayout* layoutLeft = new QVBoxLayout();
        layoutLeft->setSpacing(10);
        layoutLeft->setMargin(0);
        layoutLeft->addWidget(new SurfaceMap(q));
     /*   layoutLeft->addWidget(new CAlarmSummary(), 2);
        layoutLeft->addWidget(new CHistroyChart(), 1);*/

		//曲线图
        QVBoxLayout* layoutCenter = new QVBoxLayout();
        layoutCenter->setSpacing(10);
        layoutCenter->setMargin(0);
        layoutCenter->addWidget(new DimensionMap(q));
      /*  layoutCenter->addWidget(new CDataSummary(), 1);
        layoutCenter->addWidget(m_pDynamicsEarth, 2);
        layoutCenter->addWidget(new CCirculardialSummary(), 1);*/

		//表格
        QVBoxLayout* layoutRight = new QVBoxLayout();
        layoutRight->setSpacing(10);
        layoutRight->setMargin(0);
        layoutRight->addWidget(new DimensionTableView(q));
      /*  layoutRight->addWidget(new CPieChart(), 3);
        layoutRight->addWidget(new CDynamicAlarm(), 1);
        layoutRight->addWidget(new CRollAlarm(), 2);*/

        QHBoxLayout* layoutContent = new QHBoxLayout();
        layoutContent->setMargin(10);
        layoutContent->setSpacing(40);
        layoutContent->addLayout(layoutLeft, 2);
        layoutContent->addLayout(layoutCenter, 3);
        layoutContent->addLayout(layoutRight, 2);

        layoutMain->addWidget(m_pTitle,1);
        layoutMain->addLayout(layoutContent, 10);
        q->setLayout(layoutMain);
    }

    void initForm()
    {
        q->setAttribute(Qt::WA_StyledBackground);
        q->setWindowFlags(q->windowFlags()| Qt::FramelessWindowHint);   
    }
    VisualizeDataServer* q;
    CTitle* m_pTitle;
    QMenu* m_pMenu;
};

VisualizeDataServer::VisualizeDataServer(QWidget *parent)
    : FramelessWidget(parent)
    , ui(new Ui::VisualizeDataServerClass())
    , p(new Internal(this))
{
    ui->setupUi(this);
    p->initControl();
    p->initForm();
}

VisualizeDataServer::~VisualizeDataServer()
{
    delete ui;
}

bool VisualizeDataServer::event(QEvent* event)
{
    if (event->type() == QEvent::MouseButtonDblClick)
    {
        QMouseEvent* ev = static_cast<QMouseEvent*>(event);
        if (p->m_pTitle->geometry().contains(ev->pos()))
        {
            isFullScreen() ? showNormal() : showFullScreen();
        }
    }
    return QWidget::event(event);
}

void VisualizeDataServer::contextMenuEvent(QContextMenuEvent* event)
{
    QCursor cur = this->cursor();
    p->m_pMenu->exec(cur.pos());
    return QWidget::contextMenuEvent(event);
}

void VisualizeDataServer::showSocketSetting()
{
	SocketSettingPage *page = new SocketSettingPage();
	page->setAttribute(Qt::WA_DeleteOnClose);
	page->show();
}

void VisualizeDataServer::exitApp()
{
    qApp->exit();
}

void VisualizeDataServer::keyPressEvent(QKeyEvent* event)
{
    if (event->key() == Qt::Key_Escape)
    {
        isFullScreen() ? showNormal() : showFullScreen();
    }
}

void VisualizeDataServer::mousePressEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton) {
        if (p->m_pTitle->geometry().contains(event->pos()))
        {
            mouse_press = true;
            mousePoint = event->globalPos() - this->pos();
        }
    }
    __super::mousePressEvent(event);
}
void VisualizeDataServer::mouseMoveEvent(QMouseEvent* event)
{

    //    if(event->buttons() == Qt::LeftButton){  //如果这里写这行代码，拖动会有点问题
    if (mouse_press) {
        if (p->m_pTitle->geometry().contains(event->pos()))
        {
            move(event->globalPos() - mousePoint);
        }
       
    }
    __super::mouseMoveEvent(event);
}
void VisualizeDataServer::mouseReleaseEvent(QMouseEvent* event)
{
    mouse_press = false;
    __super::mouseReleaseEvent(event);
}
